<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<%@ include file="../common.jsp"%>
<%@ page import="org.springside.core.Constants,cn.firstsoft.firstframe.admin.*,java.util.regex.Matcher,java.util.regex.Pattern"%>
<%@ page import="java.io.*,java.util.*,org.jdom.*,org.jdom.input.*,org.jdom.output.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%
	int error_code = 0;
	String id = "";
	String name = "";
	String dbname = "";
	String dbtype = "";
	String datasource = "";
	String dbuser = "";
	String dbpassword = "";
	String maindir = "";
	String dbversion = "";
	
	String message = "";
	

	String datacenterXmlFile = System.getProperty("oa_application.config") + File.separator + "datacenter.xml";
	File datacenterFile = new File(datacenterXmlFile);
	
	//校验，合法性检查，根据安装类型和应用服务器类型判断
	String xmlFile = "";
	if ("TOMCAT".equals(Environment.getWebServerName())) { 
		xmlFile = Environment.getWebServerPath() + File.separator + "conf" + File.separator + "server.xml";
	} else if ("APUSIC".equals(Environment.getWebServerName())) {
		xmlFile = Environment.getWebServerPath() + File.separator + "config" + File.separator + "datasources.xml";
	} else {
		xmlFile = "unknown";
	}
	
	File file = new File(xmlFile);
	
	if (file.exists()) {
		SAXBuilder builder = new SAXBuilder();
		Document doc;
		Document dcdoc;
		try {
			dcdoc = builder.build(datacenterFile);
			Element datacenters = dcdoc.getRootElement();
			List datacenterList = datacenters.getChildren("datacenter");
			
			if ("TOMCAT".equals(Environment.getWebServerName())) { 
				doc = builder.build(file);
				Element server = doc.getRootElement();
				Element service = server.getChild("Service");
				Element engine = service.getChild("Engine");
				Element host = engine.getChild("Host");
				Element context = host.getChild("Context");
				List resourceList = context.getChildren("Resource");

				for (int i=0; i<resourceList.size(); i++) {
					Element resource = (Element)resourceList.get(i);
					
					//判断是否已经有这个名字了
					//如果判断出是否为EAS的数据源，需要使用名字来判断，如果名字中包含了eas开头的则标识EAS数据源

					
					String jndi_name = resource.getAttributeValue("name");
					
					id = jndi_name.substring(jndi_name.indexOf("/")+1);
					
					name = id;
					
					//这个数据源已经在应用服务器的数据源中定义，

					//需要把已经定义的内容取出并且，赋值给变量，并且重新输入

					dbuser = resource.getAttributeValue("username");
					dbpassword = resource.getAttributeValue("password");
					
					String jndi_url = resource.getAttributeValue("url");
					// jdbc:jtds:sqlserver://localhost:1433;DatabaseName=eas530
					//jdbc:jtds:sqlserver://127.0.0.1:1433/eas540;prepareSQL=2;sendStringParametersAsUnicode=false;useCursors=true
					// jdbc:oracle:thin:@//192.168.1.195:1521/orcl
					//jdbc:oracle:thin:@//192.168.1.144:1521/eas540
					if (jndi_url.indexOf("jdbc:jtds:sqlserver://")>=0) {
						String t = "jdbc:jtds:sqlserver:\\/\\/(.*?):(\\d*?)\\/(.*?);.*?$";
						Pattern p=Pattern.compile(t);
						Matcher m=p.matcher(jndi_url);
						
						if(m.find()){
							datasource = m.group(1) +":"+m.group(2);
						    dbname = m.group(3);
						}
						
						dbversion = "JTDS";
						dbtype = "sqlserver";
					} else if (jndi_url.indexOf("jdbc:oracle:thin:@")>=0) {
						/*String aurl = jndi_url.substring("jdbc:oracle:thin:@".length());
						datasource = aurl.substring(0, aurl.lastIndexOf(":"));
						dbname = aurl.substring(aurl.lastIndexOf(":")+1);
						*/
						String t = "jdbc:oracle:thin:@\\/\\/(.*?):(\\d*?)\\/(.*?)$";
						Pattern p=Pattern.compile(t);
						Matcher m=p.matcher(jndi_url);
						
						if(m.find()){
							datasource = m.group(1) +":"+m.group(2);
						    dbname = m.group(3);
						}
						dbversion = "ORACLE";
						dbtype = "oracle";
					} else {
						//不是合法数据源

						datasource = jndi_url;
						dbtype = "unknown";
					}
					
					message = message + "id:" + id + ", name:" + name + 
						", dbname:" + dbname + ", dbtype:" + dbtype +
						", datasource:" + datasource + ", dbuser:" + dbuser;
					if (!"unknown".equals(dbtype)) {
						boolean exist = false;
						for (int j=0; j<datacenterList.size(); j++) {
							Element dc = (Element)datacenterList.get(j);
							if (dc.getAttributeValue("id").equals(id)) {
								exist = true;
								break;
							}
						}
						if (exist == false) {
							//插入到datacenter.xml文件中

							Element datacenter = new Element("datacenter");
							datacenter.setAttribute("id", id);
							datacenter.setAttribute("name", name);
							datacenter.setAttribute("dbname", dbname);
							datacenter.setAttribute("dbtype", dbtype);
							datacenter.setAttribute("dbversion", dbversion);
							datacenter.setAttribute("datasource", datasource);
							datacenter.setAttribute("dbuser", dbuser);
							
							datacenter.setAttribute("dbpassword", dbpassword);
							datacenter.setAttribute("maindir", maindir);
							//通过jdbc.properties来判断，是否已经正在使用
							
							datacenter.setAttribute("isdeploy", "false");
							
							datacenters.addContent(datacenter);
							
							message  = message + " -- 导入成功！<br>\n";		
						} else {
							message  = message + " -- 已经存在！<br>\n";	
						}
					} else {
						message  = message + " -- 非支持数据类型！<br>\n";
					}

				}
			} else if ("APUSIC".equals(Environment.getWebServerName())) {
				doc = builder.build(file);
				Element datasources = doc.getRootElement();
				List resourceList = datasources.getChildren("datasource");
				
				for (int i=0; i<resourceList.size(); i++) {
					Element resource = (Element)resourceList.get(i);
					
					//判断是否已经有这个名字了
					//如果判断出是否为EAS的数据源，需要使用名字来判断，如果名字中包含了eas开头的则标识EAS数据源

					
					String jndi_name = resource.getAttributeValue("jndi-name");
					
					id = jndi_name.substring(jndi_name.indexOf("/")+1);
					
					name = id;
	
					//这个数据源已经在应用服务器的数据源中定义，

					//需要把已经定义的内容取出并且，赋值给变量，并且重新输入

					List propertyList = resource.getChildren("property");
					for (int j=0; j<propertyList.size(); j++) {
						Element property = (Element)propertyList.get(j);
						if ("user".equals(property.getAttributeValue("name"))) {
							dbuser = property.getAttributeValue("value");
						}
						if ("password".equals(property.getAttributeValue("name"))) {
							dbpassword = property.getAttributeValue("value");
						}
					}
					
					String jndi_url = resource.getAttributeValue("url");
					// jdbc:jtds:sqlserver://localhost:1433;DatabaseName=eas530
					// jdbc:oracle:thin:@//192.168.1.195:1521/orcl
					if (jndi_url.indexOf("jdbc:jtds:sqlserver://")>=0) {
						
						String t = "jdbc:jtds:sqlserver:\\/\\/(.*?):(\\d*?)\\/(.*?);.*?$";
						Pattern p=Pattern.compile(t);
						Matcher m=p.matcher(jndi_url);
						
						if(m.find()){
							datasource = m.group(1) +":"+m.group(2);
						    dbname = m.group(3);
						}
						
						dbversion = "JTDS";
						dbtype = "sqlserver";
					} else if (jndi_url.indexOf("jdbc:oracle:thin:@")>=0) {
						String t = "jdbc:oracle:thin:@\\/\\/(.*?):(\\d*?)\\/(.*?)$";
						Pattern p=Pattern.compile(t);
						Matcher m=p.matcher(jndi_url);
						
						if(m.find()){
							datasource = m.group(1) +":"+m.group(2);
						    dbname = m.group(3);
						}
						dbversion = "ORACLE";
						dbtype = "oracle";
					} else {
						//不是合法数据源

						datasource = jndi_url;
						dbtype = "unknown";
					}
					
					message = message + "id:" + id + ", name:" + name + 
						", dbname:" + dbname + ", dbtype:" + dbtype +
						", datasource:" + datasource + ", dbuser:" + dbuser;
					
					if (!"unknown".equals(dbtype)) {
						boolean exist = false;
						for (int j=0; j<datacenterList.size(); j++) {
							Element dc = (Element)datacenterList.get(j);
							if (dc.getAttributeValue("id").equals(id)) {
								exist = true;
								break;
							}
						}						
						if (exist == false) {
							//插入到datacenter.xml文件中

							Element datacenter = new Element("datacenter");
							datacenter.setAttribute("id", id);
							datacenter.setAttribute("name", name);
							datacenter.setAttribute("dbname", dbname);
							datacenter.setAttribute("dbtype", dbtype);
							datacenter.setAttribute("dbversion", dbversion);
							datacenter.setAttribute("datasource", datasource);
							datacenter.setAttribute("dbuser", dbuser);
							
							datacenter.setAttribute("dbpassword", dbpassword);
							datacenter.setAttribute("maindir", maindir);
							//通过jdbc.properties来判断，是否已经正在使用
							
							datacenter.setAttribute("isdeploy", "false");
							
							datacenters.addContent(datacenter);
							
							message  = message + " -- 导入成功！<br>\n";		
						} else {
							message  = message + " -- 已经存在！<br>\n";	
						}
					} else {
						message  = message + " -- 非支持数据类型！<br>\n";
					}
				}			
			}
			XMLOutputter outp = new XMLOutputter();
			outp.setFormat(Format.getPrettyFormat());
			outp.output(dcdoc, new FileOutputStream(datacenterFile));
			
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}		
%>
<html>
<head>
<title>管理控制台 - 数据中心</title>
<%@ include file="/common/meta2.jsp"%>
<link href="../style.css" type="text/css" rel=stylesheet>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%">
  <tr>
  	<td width="100%">
  		
  		数据中心配置文件：<%=datacenterXmlFile%><br>
  		应用服务器配置文件：<%=xmlFile %><br>
  		<%=message %>
  		导入数据源完成！<a href="index.jsp">点这里返回数据中心列表</a>
  	</td>
  </tr>
</table>
</body>
</html>
